package com.chixing.controller;


import com.chixing.pojo.BasePojo;
import com.chixing.pojo.Car;
import com.chixing.pojo.Myorder;
import com.chixing.service.CarDetailService;
import com.chixing.service.CarService;
import com.chixing.service.ReviewService;
import com.chixing.util.MongodbUtils;
import com.chixing.util.ServerResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.Formatter;
import java.util.List;

@RestController
public class ReviewController {

    @Autowired
    private ReviewService reviewService;

    @Autowired
    private CarService carService;

    @GetMapping("addReview")
    public ModelAndView addReview(@RequestParam("cusId") Integer cusId, @RequestParam("orderId")  String orderId,
                                                @RequestParam("score") Float score, @RequestParam("write")  String write,
                                               @RequestParam("carId") Integer carId, @RequestParam("shopId")  int shopId){
        System.out.println("cusId:"+cusId);
        System.out.println("orderId:"+orderId);
        System.out.println("score:"+score);
        System.out.println("write:"+write);
        System.out.println("carId:"+carId);
        System.out.println("shopId:"+shopId);
        BasePojo review = new BasePojo();
        review.setCreateTime(new Date());
        review.setMemberId(cusId);
        review.setOrderId(orderId);
        review.setScore(score);
        review.setWrite(write);
        review.setCarId(carId);
        review.setShopId(shopId);

        ServerResult serverResult = reviewService.insert(review);

        ModelAndView mav = new ModelAndView();

        if (serverResult.getCode() == 200) {
//            mav.setViewName("review_add/all_review");
//            mav.addObject("review", serverResult.getData());
             mav.setViewName("redirect:/allreview");
             return mav;
        }

        mav.setViewName("redirect:goReview/"+orderId);
        return mav;
    }


    @GetMapping("goReview/{orderId}")
    public ModelAndView goReview(@PathVariable("orderId") String orderId){
        System.out.println("orderId:"+orderId);
        ServerResult serverResult = reviewService.order2review(orderId);

        ModelAndView mav = new ModelAndView();

        if (serverResult.getCode() == 200) {
            System.out.println("======serverResult.getData()======" + serverResult.getData());

            Myorder myorder =(Myorder) serverResult.getData();
            System.out.println(myorder);
            Car car = carService.getById(myorder.getCarId());
            mav.addObject("Car",car);
            mav.addObject("Order",serverResult.getData());
        } else {
            mav.addObject("Order",serverResult.getData());
        }
        mav.setViewName("review_add/order_add_review");
        System.out.println("返回页面");
        return mav;


    }


    @GetMapping("allreview")
    public ModelAndView getAllReview(){

        System.out.println("获得所有评论");
        ServerResult serverResult = reviewService.getAllReview();

        ModelAndView mav = new ModelAndView();

        if (serverResult.getCode() == 200) {
            System.out.println("======serverResult.getData()======" + serverResult.getData());
            mav.addObject("VO",serverResult.getData());
        } else {
            mav.addObject("VO",serverResult.getData());
        }
        mav.setViewName("review_add/all_review");
        System.out.println("返回页面");
        return mav;
    }





}
